import { ref, onMounted, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getListSLA, deleteSLA, disableSLA } from '../../../api/sla'
export default function () {
    const loading = ref(false)
    const name = ref('')
    const total = ref(0)
    const tableDataList = ref(null)
    const remindModeList = ref([
        {name:'系统消息',val:'1'},
        {name:'微信',val:'2'},
        {name:'短信',val:'3'}
    ])
    const pagination = ref()

    // 获取SLA列表
    const getnetworkListRows = () => {
        loading.value = true
        let data = {
            "name": name.value,//SLA名称
            "page": pagination.value.current,//当面页码
            "limit": pagination.value.size,//每页显示数量
        }
        getListSLA(data).then(res => {
            if (res.data.code==20000) {
                tableDataList.value = res.data.data.rows
                total.value = Number(res.data.data.total)

            } else {
                if(res.data.isError)  ElMessage({
                    showClose: true,
                    message: res.data.message,
                    type: 'error',
                    duration: 4000,
                    offset: 200
                })
            }
            loading.value = false
        })
    }
    let ids = ref([])
    async function changeAll(n: number, str: string) {
        if (ids.value.length == 0) {
            ElMessage({
                message: `请选择要${str}的SLA`,
                type: 'warning',
                duration: 2000,
                offset: 200
            })
            return
        }
        ElMessageBox({ //
            title: '提示',
            message: `确认${str}的该SLA?`,
            showCancelButton: true,
            closeOnClickModal: false,
            type: 'warning',
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            autofocus: false,
        }).then(async () => {
            loading.value = true
            switch (n) {
                case 1:
                case 2:
                    await disableSLA(ids.value, n == 1 ? false : true).then(res => {
                        if(res.data.isError) ElMessage({
                            showClose: true,
                            message: res.data.message,
                            type:res.data.success? 'success':'error',
                            offset: 200
                        })
                        if (res.data.success) {
                            ids.value=[]
                            getnetworkListRows()
                        } 
                        loading.value = false
                    })
                    break;
                case 3:
                    await deleteSLA(ids.value).then(res => {
                        if(res.data.isError)   ElMessage({
                            showClose: true,
                            message: res.data.message,
                            type:res.data.success? 'success':'error',
                            offset: 200
                        })
                        if (res.data.success) {
                            ids.value=[]
                            getnetworkListRows()
                        } 
                        loading.value = false
                    })
                    break;
            }

        }).catch()
    }
    function handleChange(val) {
        ids.value = []
        val.filter(o => {
            ids.value.push(o.id)
        })
    }


    function valueChange() {
        getnetworkListRows()
    }
    return { handleChange, valueChange, changeAll, ids,remindModeList, loading, getnetworkListRows, total, pagination, tableDataList, name }
}